<?php
/* [DoYouHaoBaby!] (C)Dianniu From 2010.
   Gtk对话框($)*/

!defined('DYHB_PATH') && exit;

class DGtkDialog extends ViewGtk{

	const RESPONSE_NOTHING=0;
	private $_nResponse=self::RESPONSE_NOTHING;

	public function __construct($sName,$sTemplateFile,$sWidgetId,IViewContainer $oParent=null,TemplateGtkGlade $oTemplate=null){
		parent::__construct($sName,$sTemplateFile,$sWidgetId,$oParent,$oTemplate);
	}

	public function create(){
		$wndWidget=ViewGtk::create();
		A::INSTANCE($wndWidget,'GtkDialog',G::L('%s 的主窗体必须为有效的 GtkDialog类型 窗体。','gtk',null,__CLASS__));
		$wndWidget->connect('response', array($this,'onResponse'));
		return $wndWidget;
	}

	public function onResponse(GtkDialog $wndDialog,$nResponse){
		switch($nResponse){
			case Gtk::RESPONSE_OK :
				$this->onOK();
				break;
			case Gtk::RESPONSE_CANCEL :
				$this->onCancel();
				break;
			default :
				$this->response($nResponse);
				break;
		}
	}

	public function response($nResponse){
		A::INT($nResponse);
		$this->_nResponse=$nResponse;
	}

	public function getResponse(){
		return $this->_nResponse;
	}

	public function doDialog($bAutoDestroy=false){
		$this->showView();
		$wndMainWidget=$this->getWidget();
		A::INSTANCE($wndMainWidget,'GtkDialog',G::L('尚未初始化主窗体。','gtk'));
		$this->response(self::RESPONSE_NOTHING);// 堵塞 等待 回应
		while($this->getResponse()==self::RESPONSE_NOTHING){
			Gtk::main_iteration_do();
		}
		$this->showView(false);// 关闭对话框
		if($bAutoDestroy){// 自动销毁对话框
			$wndMainWidget->destroy();
		}
		return $this->getResponse();
	}

	public function onOk(){
		$this->response(Gtk::RESPONSE_OK);
	}

	public function onCancel(){
		$this->response(Gtk::RESPONSE_CANCEL);
	}

	public function setTitle($sTitle){
		if(!$this->getWidget()) $this->create();
		$oWndMainWidget=$this->getWidget();
		A::INSTANCE($oWndMainWidget,'GtkDialog',G::L('尚未初始化主窗体。','gtk'));
		$oWndMainWidget->set_title($sTitle);
	}

	public function getTitle(){
		if(!$this->getWidget()) $this->create();
		$oWndMainWidget=$this->getWidget();
		A::INSTANCE($oWndMainWidget,'GtkDialog',G::L('尚未初始化主窗体。','gtk'));
		return $oWndMainWidget->get_title();
	}

}
